home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfont1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.2 KB  |  410 lines

  1. /* Copyright (C) 1991, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfont1.c,v 1.8 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Type 1 and Type 4 font creation operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gxfixed.h"
  25. #include "gsmatrix.h"
  26. #include "gxdevice.h"
  27. #include "gxfont.h"
  28. #include "gxfont1.h"
  29. #include "bfont.h"
  30. #include "ialloc.h"
  31. #include "icharout.h"
  32. #include "ichar1.h"
  33. #include "idict.h"
  34. #include "idparam.h"
  35. #include "ifont1.h"
  36. #include "iname.h"        /* for name_index in enumerate_glyph */
  37. #include "store.h"
  38.  
  39. /* Type 1 font procedures (defined in zchar1.c) */
  40. extern const gs_type1_data_procs_t z1_data_procs;
  41. font_proc_glyph_info(z1_glyph_info);
  42. /* Font procedures defined here */
  43. private font_proc_font_info(z1_font_info);
  44. private font_proc_same_font(z1_same_font);
  45.  
  46. /* ------ Private utilities ------ */
  47.  
  48. private void
  49. find_zone_height(float *pmax_height, int count, const float *values)
  50. {
  51.     int i;
  52.     float zone_height;
  53.  
  54.     for (i = 0; i < count; i += 2)
  55.     if ((zone_height = values[i + 1] - values[i]) > *pmax_height)
  56.         *pmax_height = zone_height;
  57. }
  58.  
  59. /* ------ Font procedures ------ */
  60.  
  61. private int
  62. z1_enumerate_glyph(gs_font * pfont, int *pindex, gs_glyph_space_t ignored,
  63.            gs_glyph * pglyph)
  64. {
  65.     const gs_font_type1 *const pfont1 = (gs_font_type1 *)pfont;
  66.     const ref *pcsdict = &pfont_data(pfont1)->CharStrings;
  67.  
  68.     return zchar_enumerate_glyph(pcsdict, pindex, pglyph);
  69. }
  70.  
  71. /* ------ Public procedures ------ */
  72.  
  73. /* Extract pointers to internal structures. */
  74. int
  75. charstring_font_get_refs(const_os_ptr op, charstring_font_refs_t *pfr)
  76. {
  77.     check_type(*op, t_dictionary);
  78.     if (dict_find_string(op, "Private", &pfr->Private) <= 0 ||
  79.     !r_has_type(pfr->Private, t_dictionary)
  80.     )
  81.     return_error(e_invalidfont);
  82.     make_empty_array(&pfr->no_subrs, 0);
  83.     if (dict_find_string(pfr->Private, "OtherSubrs", &pfr->OtherSubrs) > 0) {
  84.     if (!r_is_array(pfr->OtherSubrs))
  85.         return_error(e_typecheck);
  86.     } else
  87.     pfr->OtherSubrs = &pfr->no_subrs;
  88.     if (dict_find_string(pfr->Private, "Subrs", &pfr->Subrs) > 0) {
  89.     if (!r_is_array(pfr->Subrs))
  90.         return_error(e_typecheck);
  91.     } else
  92.     pfr->Subrs = &pfr->no_subrs;
  93.     pfr->GlobalSubrs = &pfr->no_subrs;
  94.     return 0;
  95. }
  96.  
  97. /* Get the parameters of a CharString-based font or a FDArray entry. */
  98. int
  99. charstring_font_params(const_os_ptr op, charstring_font_refs_t *pfr,
  100.                gs_type1_data *pdata1)
  101. {
  102.     const ref *pprivate = pfr->Private;
  103.     int code;
  104.  
  105.     /* Get the rest of the information from the Private dictionary. */
  106.     if ((code = dict_int_param(pprivate, "lenIV", -1, 255, pdata1->lenIV,
  107.                    &pdata1->lenIV)) < 0 ||
  108.     (code = dict_uint_param(pprivate, "subroutineNumberBias",
  109.                 0, max_uint, pdata1->subroutineNumberBias,
  110.                 &pdata1->subroutineNumberBias)) < 0 ||
  111.     (code = dict_int_param(pprivate, "BlueFuzz", 0, 1999, 1,
  112.                    &pdata1->BlueFuzz)) < 0 ||
  113.     (code = dict_float_param(pprivate, "BlueScale", 0.039625,
  114.                  &pdata1->BlueScale)) < 0 ||
  115.     (code = dict_float_param(pprivate, "BlueShift", 7.0,
  116.                  &pdata1->BlueShift)) < 0 ||
  117.     (code = pdata1->BlueValues.count =
  118.      dict_float_array_param(pprivate, "BlueValues", max_BlueValues * 2,
  119.                 &pdata1->BlueValues.values[0], NULL)) < 0 ||
  120.     (code = dict_float_param(pprivate, "ExpansionFactor", 0.06,
  121.                  &pdata1->ExpansionFactor)) < 0 ||
  122.     (code = pdata1->FamilyBlues.count =
  123.      dict_float_array_param(pprivate, "FamilyBlues", max_FamilyBlues * 2,
  124.                 &pdata1->FamilyBlues.values[0], NULL)) < 0 ||
  125.     (code = pdata1->FamilyOtherBlues.count =
  126.      dict_float_array_param(pprivate,
  127.                 "FamilyOtherBlues", max_FamilyOtherBlues * 2,
  128.                 &pdata1->FamilyOtherBlues.values[0], NULL)) < 0 ||
  129.     (code = dict_bool_param(pprivate, "ForceBold", false,
  130.                 &pdata1->ForceBold)) < 0 ||
  131.     /*
  132.      * We've seen a few fonts with out-of-range LanguageGroup values;
  133.      * if it weren't for this, the only legal values should be 0 or 1.
  134.      */
  135.     (code = dict_int_param(pprivate, "LanguageGroup", min_int, max_int, 0,
  136.                    &pdata1->LanguageGroup)) < 0 ||
  137.     (code = pdata1->OtherBlues.count =
  138.      dict_float_array_param(pprivate, "OtherBlues", max_OtherBlues * 2,
  139.                 &pdata1->OtherBlues.values[0], NULL)) < 0 ||
  140.     (code = dict_bool_param(pprivate, "RndStemUp", true,
  141.                 &pdata1->RndStemUp)) < 0 ||
  142.     (code = pdata1->StdHW.count =
  143.      dict_float_array_check_param(pprivate, "StdHW", 1,
  144.                       &pdata1->StdHW.values[0], NULL,
  145.                       0, e_rangecheck)) < 0 ||
  146.     (code = pdata1->StdVW.count =
  147.      dict_float_array_check_param(pprivate, "StdVW", 1,
  148.                       &pdata1->StdVW.values[0], NULL,
  149.                       0, e_rangecheck)) < 0 ||
  150.     (code = pdata1->StemSnapH.count =
  151.      dict_float_array_param(pprivate, "StemSnapH", max_StemSnap,
  152.                 &pdata1->StemSnapH.values[0], NULL)) < 0 ||
  153.     (code = pdata1->StemSnapV.count =
  154.      dict_float_array_param(pprivate, "StemSnapV", max_StemSnap,
  155.                 &pdata1->StemSnapV.values[0], NULL)) < 0 ||
  156.     /* The WeightVector is in the font dictionary, not Private. */
  157.     (code = pdata1->WeightVector.count =
  158.      dict_float_array_param(op, "WeightVector", max_WeightVector,
  159.                 pdata1->WeightVector.values, NULL)) < 0
  160.     )
  161.     return code;
  162.     /*
  163.      * According to section 5.6 of the "Adobe Type 1 Font Format",
  164.      * there is a requirement that BlueScale times the maximum
  165.      * alignment zone height must be less than 1.  Some fonts
  166.      * produced by Fontographer have ridiculously large BlueScale
  167.      * values, so we force BlueScale back into range here.
  168.      */
  169.     {
  170.     float max_zone_height = 1.0;
  171.  
  172. #define SCAN_ZONE(z)\
  173.     find_zone_height(&max_zone_height, pdata1->z.count, pdata1->z.values);
  174.  
  175.     SCAN_ZONE(BlueValues);
  176.     SCAN_ZONE(OtherBlues);
  177.     SCAN_ZONE(FamilyBlues);
  178.     SCAN_ZONE(FamilyOtherBlues);
  179.  
  180. #undef SCAN_ZONE
  181.  
  182.     if (pdata1->BlueScale * max_zone_height > 1.0)
  183.         pdata1->BlueScale = 1.0 / max_zone_height;
  184.     }
  185.     /*
  186.      * According to the same Adobe book, section 5.11, only values
  187.      * 0 and 1 are allowed for LanguageGroup and we have encountered
  188.      * fonts with other values. If the value is anything else, map it to 0
  189.      * so that the remainder of the graphics library won't see an
  190.      * unexpected value.
  191.      */
  192.     if (pdata1->LanguageGroup > 1 || pdata1->LanguageGroup < 0)
  193.     pdata1->LanguageGroup = 0;
  194.     return 0;
  195. }
  196.  
  197. /* Fill in a newly built CharString-based font or FDArray entry. */
  198. int
  199. charstring_font_init(gs_font_type1 *pfont, const charstring_font_refs_t *pfr,
  200.              const gs_type1_data *pdata1)
  201. {
  202.     font_data *pdata;
  203.  
  204.     pdata = pfont_data(pfont);
  205.     pfont->data = *pdata1;
  206.     ref_assign(&pdata->u.type1.OtherSubrs, pfr->OtherSubrs);
  207.     ref_assign(&pdata->u.type1.Subrs, pfr->Subrs);
  208.     ref_assign(&pdata->u.type1.GlobalSubrs, pfr->GlobalSubrs);
  209.     pfont->data.procs = z1_data_procs;
  210.     pfont->data.proc_data = (char *)pdata;
  211.     pfont->procs.font_info = z1_font_info;
  212.     pfont->procs.same_font = z1_same_font;
  213.     pfont->procs.glyph_info = z1_glyph_info;
  214.     pfont->procs.enumerate_glyph = z1_enumerate_glyph;
  215.     pfont->procs.glyph_outline = zchar1_glyph_outline;
  216.     return 0;
  217. }
  218.  
  219. /* Build a Type 1, Type 2, or Type 4 font. */
  220. int
  221. build_charstring_font(i_ctx_t *i_ctx_p, os_ptr op, build_proc_refs *pbuild,
  222.               font_type ftype, charstring_font_refs_t *pfr,
  223.               gs_type1_data *pdata1, build_font_options_t options)
  224. {
  225.     int code = charstring_font_params(op, pfr, pdata1);
  226.     gs_font_type1 *pfont;
  227.  
  228.     if (code < 0)
  229.     return code;
  230.     code = build_gs_primitive_font(i_ctx_p, op, (gs_font_base **)&pfont, ftype,
  231.                    &st_gs_font_type1, pbuild, options);
  232.     if (code != 0)
  233.     return code;
  234.     /* This is a new font, fill it in. */
  235.     charstring_font_init(pfont, pfr, pdata1);
  236.     return define_gs_font((gs_font *)pfont);
  237. }
  238.  
  239. /* ------ Operators ------ */
  240.  
  241. /* Build a Type 1 or Type 4 font. */
  242. private int
  243. buildfont1or4(i_ctx_t *i_ctx_p, os_ptr op, build_proc_refs * pbuild,
  244.           font_type ftype, build_font_options_t options)
  245. {
  246.     charstring_font_refs_t refs;
  247.     int code = charstring_font_get_refs(op, &refs);
  248.     gs_type1_data data1;
  249.  
  250.     if (code < 0)
  251.     return code;
  252.     data1.interpret = gs_type1_interpret;
  253.     data1.subroutineNumberBias = 0;
  254.     data1.lenIV = DEFAULT_LENIV_1;
  255.     return build_charstring_font(i_ctx_p, op, pbuild, ftype, &refs, &data1,
  256.                  options);
  257. }
  258.  
  259. /* <string|name> <font_dict> .buildfont1 <string|name> <font> */
  260. /* Build a type 1 (Adobe encrypted) font. */
  261. private int
  262. zbuildfont1(i_ctx_t *i_ctx_p)
  263. {
  264.     os_ptr op = osp;
  265.     build_proc_refs build;
  266.     int code = build_proc_name_refs(&build,
  267.                     "%Type1BuildChar", "%Type1BuildGlyph");
  268.  
  269.     if (code < 0)
  270.     return code;
  271.     return buildfont1or4(i_ctx_p, op, &build, ft_encrypted,
  272.              bf_notdef_required);
  273. }
  274.  
  275. /* <string|name> <font_dict> .buildfont4 <string|name> <font> */
  276. /* Build a type 4 (disk-based Adobe encrypted) font. */
  277. private int
  278. zbuildfont4(i_ctx_t *i_ctx_p)
  279. {
  280.     os_ptr op = osp;
  281.     build_proc_refs build;
  282.     int code = build_gs_font_procs(op, &build);
  283.  
  284.     if (code < 0)
  285.     return code;
  286.     return buildfont1or4(i_ctx_p, op, &build, ft_disk_based, bf_options_none);
  287. }
  288.  
  289. /* ------ Initialization procedure ------ */
  290.  
  291. const op_def zfont1_op_defs[] =
  292. {
  293.     {"2.buildfont1", zbuildfont1},
  294.     {"2.buildfont4", zbuildfont4},
  295.     op_def_end(0)
  296. };
  297.  
  298. /* ------ Font procedures for Type 1 fonts ------ */
  299.  
  300. /* font_info procedure */
  301. private bool
  302. z1_font_info_has(const ref *pfidict, const char *key, gs_const_string *pmember)
  303. {
  304.     ref *pvalue;
  305.  
  306.     if (dict_find_string(pfidict, key, &pvalue) > 0 &&
  307.     r_has_type(pvalue, t_string)
  308.     ) {
  309.     pmember->data = pvalue->value.const_bytes;
  310.     pmember->size = r_size(pvalue);
  311.     return true;
  312.     }
  313.     return false;
  314. }
  315. private int
  316. z1_font_info(gs_font *font, const gs_point *pscale, int members,
  317.          gs_font_info_t *info)
  318. {
  319.     const gs_font_type1 *const pfont1 = (const gs_font_type1 *)font;
  320.     int code = gs_default_font_info(font, pscale, members &
  321.             ~(FONT_INFO_COPYRIGHT | FONT_INFO_NOTICE |
  322.               FONT_INFO_FAMILY_NAME | FONT_INFO_FULL_NAME),
  323.                     info);
  324.     const ref *pfdict;
  325.     ref *pfontinfo;
  326.  
  327.     if (code < 0)
  328.     return code;
  329.     pfdict = &pfont_data(pfont1)->dict;
  330.     if (dict_find_string(pfdict, "FontInfo", &pfontinfo) <= 0 ||
  331.     !r_has_type(pfontinfo, t_dictionary))
  332.     return 0;
  333.     if ((members & FONT_INFO_COPYRIGHT) &&
  334.     z1_font_info_has(pfontinfo, "Copyright", &info->Copyright))
  335.     info->members |= FONT_INFO_COPYRIGHT;
  336.     if ((members & FONT_INFO_NOTICE) &&
  337.     z1_font_info_has(pfontinfo, "Notice", &info->Notice))
  338.     info->members |= FONT_INFO_NOTICE;
  339.     if ((members & FONT_INFO_FAMILY_NAME) &&
  340.     z1_font_info_has(pfontinfo, "FamilyName", &info->FamilyName))
  341.     info->members |= FONT_INFO_FAMILY_NAME;
  342.     if ((members & FONT_INFO_FULL_NAME) &&
  343.     z1_font_info_has(pfontinfo, "FullName", &info->FullName))
  344.     info->members |= FONT_INFO_FULL_NAME;
  345.     return code;
  346. }
  347.  
  348. /* same_font procedure */
  349. private bool
  350. same_font_dict(const font_data *pdata, const font_data *podata,
  351.            const char *key)
  352. {
  353.     ref *pvalue;
  354.     bool present = dict_find_string(&pdata->dict, key, &pvalue) > 0;
  355.     ref *povalue;
  356.     bool opresent = dict_find_string(&podata->dict, key, &povalue) > 0;
  357.  
  358.     return (present == opresent &&
  359.         (present <= 0 || obj_eq(pvalue, povalue)));
  360. }
  361. private int
  362. z1_same_font(const gs_font *font, const gs_font *ofont, int mask)
  363. {
  364.     if (ofont->FontType != font->FontType)
  365.     return 0;
  366.     while (font->base != font)
  367.     font = font->base;
  368.     while (ofont->base != ofont)
  369.     ofont = ofont->base;
  370.     if (ofont == font)
  371.     return mask;
  372.     {
  373.     int same = gs_base_same_font(font, ofont, mask);
  374.     int check = mask & ~same;
  375.     const gs_font_type1 *const pfont1 = (const gs_font_type1 *)font;
  376.     const font_data *const pdata = pfont_data(pfont1);
  377.     const gs_font_type1 *pofont1 = (const gs_font_type1 *)ofont;
  378.     const font_data *const podata = pfont_data(pofont1);
  379.  
  380.     if ((check & (FONT_SAME_OUTLINES | FONT_SAME_METRICS)) &&
  381.         !memcmp(&pofont1->data.procs, &z1_data_procs, sizeof(z1_data_procs)) &&
  382.         obj_eq(&pdata->CharStrings, &podata->CharStrings) &&
  383.         /*
  384.          * We use same_font_dict for convenience: we know that
  385.          * both fonts do have Private dictionaries.
  386.          */
  387.         same_font_dict(pdata, podata, "Private")
  388.         )
  389.         same |= FONT_SAME_OUTLINES;
  390.  
  391.     if ((check & FONT_SAME_METRICS) && (same & FONT_SAME_OUTLINES) &&
  392.         !memcmp(&pofont1->data.procs, &z1_data_procs, sizeof(z1_data_procs)) &&
  393.         /* Metrics may be affected by CDevProc, Metrics, Metrics2. */
  394.         same_font_dict(pdata, podata, "Metrics") &&
  395.         same_font_dict(pdata, podata, "Metrics2") &&
  396.         same_font_dict(pdata, podata, "CDevProc")
  397.         )
  398.         same |= FONT_SAME_METRICS;
  399.  
  400.     if ((check & FONT_SAME_ENCODING) &&
  401.         pofont1->procs.same_font == z1_same_font &&
  402.         obj_eq(&pdata->Encoding, &podata->Encoding)
  403.         )
  404.         same |= FONT_SAME_ENCODING;
  405.  
  406.     return same & mask;
  407.     }
  408. }
  409.  
  410.